﻿#include "FITKAbaqusHDF5Writer.h"
#include "FITKAbaqusHDF5AdaptorCase.h"
#include "FITKAbaqusHDF5AdaptorJob.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITK_Kernel/FITKCore/FITKAbstractDataManager.hpp"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCaseManager.h"
#include "FITK_Component/FITKAbaqusData/FITKJob.h"
#include "FITK_Component/FITKAbaqusData/FITKJobManager.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusData.h"
#include "H5Cpp.h"
#include <QDebug>
namespace IO
{
    void FITKAbaqusHDF5Writer::setDataCaseManager(AbaqusData::FITKDataCaseManager * caseManager)
    {
        _caseManager = caseManager;
    }

    void FITKAbaqusHDF5Writer::setJobManager(AbaqusData::FITKJobManager * jobManager)
    {
        _jobManager = jobManager;
    }

    void FITKAbaqusHDF5Writer::run()
    {
        *_resultMark = false;
        auto abaqusData = dynamic_cast<AbaqusData::FITKAbaqusData*>(_data);
        //if (!abaqusData || !_h5File) return;
        _caseManager = abaqusData->getDataCaseManager();
        _jobManager = abaqusData->getJobManager();
        if (!_caseManager || !_jobManager) return;
        *_resultMark = true;
        *_resultMark &= writeVersion();
        *_resultMark &= writeDataCases();
        *_resultMark &= writeJobs();

        this->sendCalculateProgress(100);
    }

    bool FITKAbaqusHDF5Writer::writeDataCases()
    {
        bool isW = true;
        // create cases group
        H5::Group casesGroup = _h5File->createGroup("Cases");

        int count = _caseManager->getDataCount();
        for (int i = 0; i < count; i++)
        {
            auto dataCase = _caseManager->getDataByIndex(i);
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusHDF5AdaptorCase>("HDF5", dataCase);
            if (adaptor == nullptr) return false;
            adaptor->setH5GroupData(casesGroup);
            adaptor->setDataObject(dataCase);
            adaptor->setFileWriter(this);
            bool w = adaptor->adaptW();
            if (!w) consoleMessage(3, QString("Write Case Errors, CaseName : %1").arg(dataCase->getDataObjectName()));
            isW &= w;
            delete adaptor;
        }
        return isW;
    }

    bool FITKAbaqusHDF5Writer::writeJobs()
    {
        bool isW = true;
        // create Jobs group
        H5::Group JobGroup = _h5File->createGroup("Jobs");

        int jobCount = _jobManager->getDataCount();
        for (int i = 0; i < jobCount; ++i)
        {
            auto job = _jobManager->getDataByIndex(i);
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusHDF5AdaptorJob>("HDF5", "AbaqusData::FITKJob");
            if (adaptor == nullptr) return false;
            adaptor->setH5GroupData(JobGroup);
            adaptor->setDataObject(job);
            adaptor->setFileWriter(this);
            bool w = adaptor->adaptW();
            if (!w) consoleMessage(3, QString("Write Job Errors"));
            isW &= w;
            delete adaptor;
        }
        return isW;
    }

    bool FITKAbaqusHDF5Writer::writeVersion()
    {
        QString name = "Version";
        H5::Group versionGroup = _h5File->createGroup(name.toStdString());
        hsize_t       dim[2] = { 1, 1 };
        H5::DataSpace SpaceThree(2, dim);
        H5::Attribute transAttr = versionGroup.createAttribute(name.toStdString(), H5::PredType::NATIVE_DOUBLE, SpaceThree);
        transAttr.write(H5::PredType::NATIVE_DOUBLE, &_version);
        return true;
    }

    void FITKAbaqusHDF5Writer::sendCalculateProgress(int Progress)
    {
    }

    void FITKAbaqusHDF5Writer::consoleMessage(int level, const QString & str)
    {
        qDebug() << str;
        //判断消息等级
        switch (level)
        {
        case 1:AppFrame::FITKMessageNormal(str); break;
        case 2: AppFrame::FITKMessageWarning(str); break;
        case 3:AppFrame::FITKMessageError(str); break;
        default: AppFrame::FITKMessageError(str); break;
        }
    }

}
